<template>
  <div>
    <FilterTable
      ref="table"
      :fetchapi="fetchApi"
      :columns="columns"
      :params="fetchParams"
      :datakey="'rows'"
      :height="600"
      :isSelectColumn="false"
      :isColumnFilter="false"
      :isToperInfo="false"
      :onColumnsChange="columns => (this.columns = columns)"
    ></FilterTable>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { getRemindRecord } from '@/api/customerRepair/customerCare/vehicleRemind';
import { getBrand, getSeries, getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import { getDealerUser } from '@/api/part/partInventory/partInventory/index.js';
export default {
  mixins: [dictionary],
  props: {
    vin: String,
    ownerNo: String,
    remindSource: Number // 1：保养到期提醒；2：定期保养提醒；3：验车到期提醒；4：车主生日提醒；5：服务活动邀约；6：维修建议查询；7:首保提醒 8: 延保提醒
  },
  data() {
    this.BaseTable = null;
    return {
      fetchApi: getRemindRecord,
      fetchParams: { xhrAbort: false, vin: this.vin, ownerNo: this.ownerNo, remindSource: this.remindSource },
      columns: this.createTableColumns()
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.queryBrand();
    this.querySeries();
    this.queryModel();
  },
  created() {
    this.getEmp();
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商',
          dataIndex: 'OWNER_CODE'
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE'
        },
        {
          title: '车主',
          dataIndex: 'OWNER_NAME'
        },
        {
          title: '品牌',
          dataIndex: 'BRAND',
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '车系',
          dataIndex: 'SERIES',
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '车型',
          dataIndex: 'MODEL',
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '车主性质',
          dataIndex: 'OWNER_PROPERTY',
          render: props => {
            return <span>{this.createDictText(props.row.OWNER_PROPERTY, '8011')}</span>;
          }
        },
        {
          title: '提醒时间',
          dataIndex: 'REMIND_DATE',
          sorter: false,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '提醒方式',
          dataIndex: 'REMIND_WAY',
          render: props => {
            return <span>{this.createDictText(props.row.REMIND_WAY, '8081')}</span>;
          }
        },
        {
          title: '提醒内容 ',
          dataIndex: 'REMIND_CONTENT',
          editable: false
        },
        {
          title: '提醒人',
          dataIndex: 'REMINDER',
          editable: false,
          filterType: 'input',
          render: props => {
            return <span>{this.getEmployeeName(props.row.REMINDER)}</span>;
          }
        },
        {
          title: '提醒状态',
          dataIndex: 'REMIND_STATUS',
          render: props => {
            return <span>{this.createDictText(props.row.REMIND_STATUS, '8057')}</span>;
          }
        },
        {
          title: '醒失败原因',
          dataIndex: 'REMIND_FAIL_REASON',
          render: props => {
            return <span>{this.createDictText(props.row.REMIND_FAIL_REASON, '8069')}</span>;
          }
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          editable: false
        }
      ];
    },
    // 查询所有品牌(供table使用)
    async queryBrand() {
      const res = await getBrand();
      this.columns.find(x => x.dataIndex === 'BRAND').editItems = res.data.map(x => ({ text: x.BRAND_NAME, value: x.BRAND_ID + '' }));
    },
    // 查询所有车系(供table使用)
    async querySeries() {
      const res = await getSeries();
      this.columns.find(x => x.dataIndex === 'SERIES').editItems = res.data.map(x => ({ text: x.SERIES_NAME, value: x.SERIES_ID + '' }));
    },
    // 查询所有车型(供table使用)
    async queryModel() {
      const res = await getModel();
      this.columns.find(x => x.dataIndex === 'MODEL').editItems = res.data.map(x => ({ text: x.MODEL_NAME, value: x.MODEL_ID + '' }));
    },
    // 获取员工信息并转义
    async getEmp() {
      const res = await getDealerUser();
      this.serviceAdvisorData = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
      // this.columns.find(x => x.dataIndex === 'CREATED_BY').editItems = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
      // this.columns.find(x => x.dataIndex === 'LOCK_USER').editItems = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
    },
    // 转换数据
    getEmployeeName(userId) {
      // 如果table表格不为空
      if (this.serviceAdvisorData != undefined) {
        const item = this.serviceAdvisorData.find(x => {
          return x.value == userId;
        });
        return item ? item.text : '';
      }

    }
  }
};
</script>
